/**
 * Copyright 2017 JessYan
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lib.fastkit.utils.permission.custom;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.packageinfo.PackageInfoUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import timber.log.Timber;


/**
 * ================================================
 * 权限请求工具类
 *
 * @see <a href="https://github.com/JessYanCoding/MVPArms/wiki#3.9">PermissionUtil wiki 官方文档</a>
 * Created by JessYan on 17/10/2016 10:09
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class PermissionUtil {

    public static final String TAG = "Permission";

    public static RxPermissions rxPermissions;

    public static PermissionUtil permissionUtil;

    private static Activity mactivity;

    public static PermissionUtil getInstance(Activity activity) {

        if (permissionUtil == null) {
            rxPermissions = new RxPermissions(activity);
            permissionUtil = new PermissionUtil();

            mactivity = activity;

        }

        return permissionUtil;

    }

    public interface RequestPermission {
        /**
         * 权限请求成功
         */
        void onRequestPermissionSuccess();

        /**
         * 用户拒绝了权限请求, 权限请求失败, 但还可以继续请求该权限
         *
         * @param
         */
        void onRequestPermissionFailure();
//
//        /**
//         * 用户拒绝了权限请求并且用户选择了以后不再询问, 权限请求失败, 这时将不能继续请求该权限, 需要提示用户进入设置页面打开该权限
//         *
//         * @param permissions 请求失败的权限名
//         */
//        void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions);
    }


    public static void requestPermission(RxPermissions rxPermissions, final RequestPermission requestPermission,
                                         String... permissions) {
        if (permissions == null || permissions.length == 0) return;
        if (requestPermission == null) return;
        if (rxPermissions == null) return;

        List<String> needRequest = new ArrayList<>();

        //过滤掉已经申请过的权限
        if (permissions != null && permissions.length > 0) {

            try {
                for (int i = 0; i < permissions.length; i++) {
                    if (rxPermissions != null && StringUtil.isNotEmpty(permissions[i], true)) {
                        //isGranted相当于授权的意思 --- rxPermissions.isGranted 返回true，代表已经授权该权限
                        if (!rxPermissions.isGranted(permissions[i])) {
                            LogUtil.e("还需要的权限为：" + permissions[i]);
                            needRequest.add(permissions[i]);
                        }
                    }
                }
            } catch (Exception e) {

                LogUtil.e("出现异常，异常问题为：" + e.toString());
            } finally {

                for (int i = 0; i < permissions.length; i++) {
                    needRequest.add(permissions[i]);
                }
            }

        }

        if (needRequest.isEmpty()) {//全部权限都已经申请过，直接执行操作
            requestPermission.onRequestPermissionSuccess();
        } else {//没有申请过,则开始申请

            rxPermissions
                    .requestEachCombined(needRequest.toArray(new String[needRequest.size()]))
                    .buffer(permissions.length)
                    .subscribe(new PermissionsSubscriber<List<Permission>>() {
                        @Override
                        public void onNext(@NonNull List<Permission> permissions) {
                            for (Permission p : permissions) {
                                if (!p.granted) {
                                    if (p.shouldShowRequestPermissionRationale) {
                                        Timber.tag(TAG).d("Request permissions failure");
                                        requestPermission.onRequestPermissionFailure();
                                        onRequestPermissionFailure(Arrays.asList(p.name));
                                        return;
                                    } else {
                                        Timber.tag(TAG).d("Request permissions failure with ask never again");

                                        requestPermission.onRequestPermissionFailure();
                                        onRequestPermissionFailure(Arrays.asList(p.name));

                                        return;
                                    }
                                }
                            }
                            Timber.tag(TAG).d("Request permissions success");
                            requestPermission.onRequestPermissionSuccess();
                        }
                    });
        }

    }

    public static void requestPermission1(final RequestPermission requestPermission) {

        rxPermissions.requestEachCombined(Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.MODIFY_AUDIO_SETTINGS
                , Manifest.permission.RECORD_AUDIO
                , Manifest.permission.CAMERA
                , Manifest.permission.WAKE_LOCK)
//                .subscribe(new PermissionsSubscriber<Permission>() {
//                    @Override
//                    public void onNext(Permission permission) {
//
//                        if (permission.granted){
//
//                            LogUtil.e("权限请求成功");
//
//                        }else {
////                            requestPermission1();
//
//                            getAppDetailSettingIntent();
//
//
//                        }
//                    }
//
//                });
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {

                            LogUtil.e("权限请求成功");
                            requestPermission.onRequestPermissionSuccess();

                        } else {
//                            requestPermission1();

//                            getAppDetailSettingIntent();

                            String sdk = android.os.Build.VERSION.SDK; // SDK号

                            String model = android.os.Build.MODEL; // 手机型号

                            String release = android.os.Build.VERSION.RELEASE; // android系统版本号
                            String brand = Build.BRAND;//手机厂商
                            if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {
                                PermissionPhoneJudge.gotoMiuiPermission(mactivity);//小米
                            } else if (TextUtils.equals(brand.toLowerCase(), "meizu")) {
                                PermissionPhoneJudge.gotoMeizuPermission(mactivity);
                            } else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {
                                PermissionPhoneJudge.gotoHuaweiPermission(mactivity);
                            } else {
                                mactivity.startActivity(PermissionPhoneJudge.getAppDetailSettingIntent(mactivity));
                            }
                        }

                    }
                });

    }

    /**
     * 请求外部存储的权限
     */
    public static void externalStorage(RequestPermission requestPermission) {
        requestPermission(rxPermissions, requestPermission,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    /**
     * 请求直播需要的权限
     *
     * @param requestPermission
     */
    public static void externalZhiBo(RequestPermission requestPermission) {
        requestPermission(rxPermissions, requestPermission
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.MODIFY_AUDIO_SETTINGS
                , Manifest.permission.RECORD_AUDIO
                , Manifest.permission.CAMERA
                , Manifest.permission.WAKE_LOCK
                , Manifest.permission.CALL_PHONE
                , Manifest.permission.READ_CONTACTS
                , Manifest.permission.INTERNET
                , Manifest.permission.READ_PHONE_STATE
        );
    }

    /**
     * 请求直播需要的权限
     *
     * @param requestPermission
     */
    public static void externalMain(RequestPermission requestPermission) {
        requestPermission(rxPermissions, requestPermission
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.RECORD_AUDIO
                , Manifest.permission.CAMERA
                , Manifest.permission.INTERNET
                , Manifest.permission.READ_PHONE_STATE
        );
    }


    /**
     * 支付
     *
     * @param requestPermission
     */
    public static void externalZhiFu(RequestPermission requestPermission) {
        requestPermission(rxPermissions, requestPermission, Manifest.permission.READ_PHONE_STATE
                , Manifest.permission.WRITE_EXTERNAL_STORAGE

        );
    }


    /**
     * 语音
     *
     * @param requestPermission
     */

    public static void externalAudio(RequestPermission requestPermission) {
        requestPermission(rxPermissions, requestPermission,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.MODIFY_AUDIO_SETTINGS
                , Manifest.permission.RECORD_AUDIO
                , Manifest.permission.CAMERA
                , Manifest.permission.WAKE_LOCK
                , Manifest.permission.READ_PHONE_STATE
        );
    }


    private static void onRequestPermissionFailure(List<String> strings) {
        for (String str : strings) {
            switch (str) {
                case "android.permission.SYSTEM_ALERT_WINDOW": {
                    Toast.makeText(mactivity, "禁止弹出框权限，部分功能将无法使用！", Toast.LENGTH_SHORT).show();
                    break;
                }
                case "android.permission.WRITE_EXTERNAL_STORAGE": {
                    Toast.makeText(mactivity, "禁止访问存储权限，部分功能将无法使用！", Toast.LENGTH_SHORT).show();
                    break;
                }

                case "android.permission.CAMERA": {
                    Toast.makeText(mactivity, "禁止访问摄像头，部分功能将无法使用！", Toast.LENGTH_SHORT).show();
                    break;
                }

                case "android.permission.RECORD_AUDIO": {
                    Toast.makeText(mactivity, "禁止访问录音，部分功能将无法使用！", Toast.LENGTH_SHORT).show();

                    break;
                }
            }
        }
    }
}

